<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Area extends Controller
{
    /**
     * 获取省级数据
     *
     * @return \think\Response
     */
    public function ajax_province()
    {
        $whereData = ['is_open' => config('code.status_normal')];
        $whereData = ['area_type' => 1];
        $data = model('area')->field('`area_id` `id`,`area_name` `name`')->where($whereData)->order(['area_sort' =>  'DESC','area_id' => 'ASC'])->select();
        if(!empty($data)){
            $return['status'] = 1;
            $return['list'] = $data;
        }else{
            $return['status'] = 0;
            $return['info'] = '没有开启省份，请先开启!';
        }
        return json_encode($return);
    }

    /**
     * 获取市级数据.
     *
     * @return \think\Response
     */
    public function ajax_city()
    {
        $condition_area['area_pid'] = intval($_POST['id']);
        if($_POST['id']==0){
            $return['error'] = 0;
            $return['list'] = array();
            exit(json_encode($return));
        }
        $data = model('area')->field('`area_id` `id`,`area_name` `name`')->where($condition_area)->order(['area_sort' =>  'DESC','area_id' => 'ASC'])->select();
        if(!empty($data)){
            $return['status'] = 1;
            $return['list'] = $data;
        }else{
            $return['status'] = 0;
            $return['info'] = '［ <b>'.$_POST['name'] .'</b> ］ 省份下没有已开启的城市！请先开启城市或删除此省份';
        }
        return json_encode($return);
    }

    /**
     * 获取区县信息
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function ajax_area()
    {
        $condition_area['area_pid'] = intval($_POST['id']);
        if($_POST['id']==0){
            $return['error'] = 0;
            $return['list'] = array();
            exit(json_encode($return));
        }
        $data = model('area')->field('`area_id` `id`,`area_name` `name`')->where($condition_area)->order(['area_sort' =>  'DESC','area_id' => 'ASC'])->select();
        if(!empty($data)){
            $return['status'] = 1;
            $return['list'] = $data;
        }else{
            $return['status'] = 0;
            $return['info'] = '［ <b>'.$_POST['name'] .'</b> ］ 省份下没有已开启的城市！请先开启城市或删除此省份';
        }
        return json_encode($return);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
